package com.asman.worker.dialog

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.net.Uri
import android.os.Bundle
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.fragment.app.FragmentManager
import com.asman.worker.R
import kotlinx.android.synthetic.main.fragment_service_phone_dialog.*
import showToast

/**
 * author : ybk
 * date   : 2019-07-08 10:27
 * desc   : 拨打电话dialog
 */
class ServicePhoneDialogFragment : AppCompatDialogFragment() {
    companion object {
        private const val TITLE = "title"
        private const val PHONE_NUMBER = "phoneNumber"

        fun newInstance(title: String? = null, phoneNumber: String) = ServicePhoneDialogFragment().apply {
            arguments = Bundle().apply {
                putString(TITLE, title)
                putString(PHONE_NUMBER, phoneNumber)
            }
        }
    }

    private var title: String? = null
    private var phoneNumber: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            title = it.getString(TITLE)
            phoneNumber = it.getString(PHONE_NUMBER)
        }
//        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        isCancelable = false
    }

    override fun onStart() {
        super.onStart()
        val window = dialog?.window
        val windowParams = window!!.attributes
        windowParams.dimAmount = 0.2f//背景完全透明
        window.attributes = windowParams
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        dialog?.setOnKeyListener { dialog, keyCode, event ->
            when (keyCode) {
                KeyEvent.KEYCODE_BACK -> {
                    dismiss()
                    true
                }
                else -> true
            }
        }
        return inflater.inflate(R.layout.fragment_service_phone_dialog, container, false)
    }

    @SuppressLint("SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (title.isNullOrEmpty()) {
            tv_phone.text = "$phoneNumber"
        } else {
            tv_phone.text = "$title：$phoneNumber"
        }
//            tv_content.visibility = View.GONE
        layout_ok.setOnClickListener {
            callBack?.onOkClick()
            try {
                context?.startActivity(Intent(Intent.ACTION_DIAL, Uri.parse("tel:$phoneNumber")))
            } catch (e: Exception) {
                e.printStackTrace()
                showToast("设备不支持拨打电话")
            }
            dismiss()
        }
        btn_cancel.setOnClickListener {
            callBack?.onCancelClick()
            dismiss()
        }
    }

    fun show(manager: FragmentManager, tag: String?, callBack: CallBack) {
        super.show(manager, tag)
        this.callBack = callBack
    }

    var callBack: CallBack? = null

    interface CallBack {
        fun onCancelClick()
        fun onOkClick()
    }

}
